#include "srv_asynch_man.h"
#include "Task_Base/CriticalSection.h"
#include "Include/Srv_Service_Handler.h"
#include "asynch_read_queue.h"
#include "asynch_write_queue.h"
#include "asynch_return_queue.h"
#include "epoll_impl.h"
#include <string.h>
Srv_Asynch_Man::Srv_Asynch_Man()
{
    m_poCriSection_socket   = new CCriticalSection;

    m_pAsynch_Return_Queue    = new Asynch_Return_Queue(this);

    m_pAsynch_Read_Queue    = new Asynch_Read_Queue(this);

    m_pAsynch_write_queue   = new Asynch_Write_Queue(this);

    m_pAsynch_Return_Queue->activate();

    m_pAsynch_Read_Queue->activate();

    m_pAsynch_write_queue->activate();

    m_pEpoll_Impl = new Epoll_Impl(this);
}

Srv_Asynch_Man::~Srv_Asynch_Man(void)
{
    all_delete_service_handler();

    delete m_pAsynch_Return_Queue;

    delete m_pAsynch_Read_Queue;

    delete m_pAsynch_write_queue;

    delete m_pEpoll_Impl;	//关闭epoll任务

    delete m_poCriSection_socket;
}

int Srv_Asynch_Man::Open(const char *pAddr,int nPort)
{
    if (m_pEpoll_Impl)
    {
       m_pEpoll_Impl->epoll_Init(pAddr,nPort);
    }
    return 1;
}

Srv_Service_Handler *Srv_Asynch_Man::make_handler (int client_socket)//工厂函数 创建连接
{
    return new Srv_Service_Handler(client_socket,this);
}

void Srv_Asynch_Man::add_service_handler(int client_socket)		//当连接到来
{
    if(m_pAsynch_Return_Queue)
        m_pAsynch_Return_Queue->atq_add_service_handler(client_socket);//

    if(m_pAsynch_write_queue)
        m_pAsynch_write_queue->awq_add_service_handler(client_socket);//

    CCriticalAutoLock loAutoLock(*m_poCriSection_socket);

    map_socket_handler[client_socket] =make_handler(client_socket) ;

    if(map_socket_handler[client_socket])
    {
        map_socket_handler[client_socket]->Open();//开启请求
    }
}

void Srv_Asynch_Man::all_delete_service_handler()
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_socket);

    std::map<int,Srv_Service_Handler*>::iterator it_handle =map_socket_handler.begin();
    while(it_handle != map_socket_handler.end())
    {
        it_handle->second->close();  //强转为非const指针

        if (m_pEpoll_Impl)
                m_pEpoll_Impl->destroy_connection_prc(it_handle->first);

        if(m_pAsynch_Return_Queue)
            m_pAsynch_Return_Queue->atq_post_delete_socket(it_handle->first);//

        if(m_pAsynch_write_queue)
            m_pAsynch_write_queue->awq_post_delete_socket(it_handle->first);//

        delete it_handle->second;

        map_socket_handler.erase(it_handle);

        it_handle = map_socket_handler.begin();
    }
}

void Srv_Asynch_Man::delete_service_handler(int client_socket)	//当连接结束
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_socket);

    std::map<int,Srv_Service_Handler*>::iterator it_handle = map_socket_handler.find(client_socket);
    if (it_handle != map_socket_handler.end())
    {
        it_handle->second->close();  //强转为非const指针

        if(m_pAsynch_Return_Queue)
            m_pAsynch_Return_Queue->atq_post_delete_socket(client_socket);//

        if(m_pAsynch_write_queue)
            m_pAsynch_write_queue->awq_post_delete_socket(client_socket);//

        delete it_handle->second;

        map_socket_handler.erase(it_handle);
    }
}

void Srv_Asynch_Man::destroy_connection(int client_socket)
{
    delete_service_handler(client_socket);
    if (m_pEpoll_Impl)
            m_pEpoll_Impl->destroy_connection_prc(client_socket);

}

void Srv_Asynch_Man::post_socket_send(int client_socket)
{
    if(m_pAsynch_write_queue)
        m_pAsynch_write_queue->awq_post_send_socket(client_socket);
}

void Srv_Asynch_Man::post_socket_read(int client_socket)
{
    if(m_pAsynch_Read_Queue)
        m_pAsynch_Read_Queue->arq_post_read_socket(client_socket);
}

void Srv_Asynch_Man::append_write_buf(int client_socket,const char *pBuf,int nlen)
{
    if(m_pAsynch_write_queue)
        m_pAsynch_write_queue->awq_append_write_buf(client_socket,pBuf,nlen);
}

void Srv_Asynch_Man::append_read_buf(int client_socket ,string & send_buf)
{
    if(m_pAsynch_Return_Queue)
        m_pAsynch_Return_Queue->atq_append_read_buf(client_socket,send_buf.data(),send_buf.length());
}

Srv_Service_Handler * Srv_Asynch_Man::find_service_handler( int client_socket )
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_socket);

    std::map<int,Srv_Service_Handler*>::iterator it =map_socket_handler.find(client_socket);

    if (it != map_socket_handler.end())
    {
         return it->second;
    }
    return	NULL;
}

bool Srv_Asynch_Man::handle_send_passive(int client_socket, string &send_buf)
{
    if (m_pEpoll_Impl)
    {
        return m_pEpoll_Impl->send_passive_prc(client_socket,send_buf);
    }
    return false ;
}

bool Srv_Asynch_Man::handle_send_active(int client_socket, string &send_buf)
{
    if (m_pEpoll_Impl)
    {
        return m_pEpoll_Impl->send_active_prc(client_socket,send_buf);
    }
    return false ;
}

void Srv_Asynch_Man::handle_read_passive(int client_socket, string &read_buf)
{
    if (m_pEpoll_Impl)
    {
        return m_pEpoll_Impl->read_passive_prc(client_socket,read_buf);
    }
}

void Srv_Asynch_Man::handle_read(int client_socket,const char *pBuf,int nlen)
{
    Srv_Service_Handler * ptr_handle	=find_service_handler(client_socket);
    if(ptr_handle)
       ptr_handle->handle_read_stream(pBuf,nlen);
}
